<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/10/17
  Time: 10:29
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../../include/head.jsp">
    <jsp:param name="title" value="event_subscribe_list"/>
</jsp:include>

<jsp:include page="../../include/tenant_header.jsp"/>

<jsp:include page="../../include/tenant_sidebar.jsp"/>
<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.tenant.event.subscribe"/></h4>
        <a class="btn btn-add" href="/partner/${sessionScope['siemens.multitenant.login.user'].getTenant()}/event/subscription#eventSub" style="margin-left:84px;">
            <i class="fa fa-plus" style="margin-right: 3px"></i><spring:message code="label.add.subscribe"/>
        </a>
        <div class="form-group has-success has-feedback" style="float: right;margin-right: 40px;">
            <input type="text" placeholder="<spring:message code="label.admin.search"/>" class="form-control" id="inputSuccess2" style="border-radius:5px;border-color:#aabfcb">
            <span class="glyphicon glyphicon-search form-control-feedback"></span>
        </div>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body"  style="padding: 0">
                <table class="table" id="event_list">
                    <thead>
                    <tr>
                        <th><spring:message code="label.event.name"/></th>
                        <th><spring:message code="label.event.type"/></th>
                        <th><spring:message code="label.tenant.create.description"/></th>
                        <th><spring:message code="label.tenant.device.subscribe"/></th>
                        <th><spring:message code="label.operation"/></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        var table = $('#event_list').DataTable({
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            aLengthMenu: [15],
            ajax: {
//                url: '/partner/{tenant}/api/v1/event/list'.replace("{tenant}",getKeyByUrl()),
                url: '/partner/{tenant}/api/v1/eventsByDataTable'.replace("{tenant}",getKeyByUrl()),
                dataSrc: 'data'
            },
            columns: [
                {
                    data: 'event_name'
                },
                {
                    data: 'event_type'
                },
                {
                    data: 'event_desc'
                },
                {
                    data: 'subscription'
                },
                {
                    data: 'event_id',
                    "render": function (data, type, full, meta) {
                        var divString = '<div class="sie-btn-group" style="text-align: center">' +
                                '<a href="javascript:editEvent(\'' +data + '\');"><i class="sie-btn-edit"></i><span><spring:message code="label.user.edit"/></span></a><!--' +
                                '--><a href="javascript:deleteEvent(\'' +data + '\');" style="margin-left: 20px"><i class="sie-btn-delete"></i><span><spring:message code="label.user.delete"/></span></a></div>';
                        return divString;
                    }
                }
            ],
            "language": {url: '/lang/datatable.chs.json'}
        });
        $('#inputSuccess2').bind('keyup', function (e) {
            table.search(this.value).draw();
        });
    });

    function editEvent(data){
       window.location.href=("/partner/{tenant}/even/modify?eventId="+data+"#eventSub").replace("{tenant}",getKeyByUrl());
    }

    function deleteEvent(data){
        swal({
            title: '<spring:message code="label.user.delete"/>',
            text: '<spring:message code="label.delete.event"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                url:"/partner/{tenant}/api/v1/event/delete".replace("{tenant}",getKeyByUrl()),
                type:"post",
                data:{eventId:data},
                success:function(){
                    $('#event_list').DataTable().ajax.reload();
                },
                error:function(e){
                    console.log(e);
                }
            });
        });
    }
</script>
